
/// <reference path="./../Define.ts" />
/// <reference path="./../module/ModuleMgr.ts" />
/// <reference path="./../module/BaseModule.ts" />
/// <reference path="./../utils/Sets.ts" />

namespace apeng {

    /**暂时使用静态数据 */
    @moduleMgr.add(EModuleType.server)
    export class ServerModule extends BaseModule {

        /**游戏排行  */
        public gameList(param: IServerParamBase<IServerGameItem[]>) {
            setTimeout(() => {
                let data = this._parse("game_rank_sort")

                if (param.success)
                    param.success(data)

                if (param.complete)
                    param.complete()
            }, 200)
        }

        /**最新排行  */
        public newList(param: IServerParamBase<IServerGameItem[]>) {
            setTimeout(() => {
                let data = this._parse("new_rank_sort")

                if (param.success)
                    param.success(data)

                if (param.complete)
                    param.complete()
            }, 100)
        }

        /**最新排行table上方的4个推荐位  */
        public newUpList(param: IServerParamBase<IServerGameItem[]>) {
            setTimeout(() => {
                let data = this._parse("new_up_rank_sort")

                if (data.length > 4)
                    data.length = 4

                if (param.success)
                    param.success(data)

                if (param.complete)
                    param.complete()
            }, 1000)
        }


        /**进入界面时 请求的接口  */
        public enterData(param: IServerGameEnterData) {
            setTimeout(() => {
                let config = _config.obj.game_item[param.gameId as any as number]
                let data = config.disable == 1

                if (param.success)
                    param.success({ disable: data })

                if (param.complete)
                    param.complete()
            }, 100)
        }

        private _parse(sortKey: string) {
            let data: IServerGameItem[] = []

            let ids: number[] = []
            for (let config of _config.arr.game_item)
                if (config.disable != 1)
                    ids.push(config.id)

            Sets.sortMax(ids, v => _config.obj.game_item[v][sortKey])

            for (let id of ids)
                data.push(this.createItem(id))

            return data
        }

        private createItem(id: number) {
            let config = _config.obj.game_item[id]
            let data: IServerGameItem = {
                name: _language.get(config.id_name),
                describe: _language.get(config.id_describe),
                people_count: config.people_count,
                link_url: config.link_url,
                icon_url: config.icon_url,
            }

            return data
        }


    }

}